<?php
class ControllerModuleAdvertisement extends Controller {
	private $error = array();
	
	public function index() {
		$this->load->model('fido/advertisement');
		$this->model_fido_advertisement->checkAdvertisements();
		$this->load->language('module/advertisement');
		$this->document->title = $this->language->get('heading_title');
		$this->load->model('setting/setting');
		if (($this->request->server['REQUEST_METHOD'] == 'POST') && ($this->validate())) {
			$this->model_setting_setting->editSetting('advertisement', $this->request->post);		
			$this->session->data['success'] = $this->language->get('text_success');
			$this->redirect(HTTPS_SERVER . 'index.php?route=extension/module&token=' . $this->session->data['token']);
		}
		$this->getList();
	}

	public function insert() {
		$this->load->language('module/advertisement');
		$this->document->title = $this->language->get('heading_title');
		$this->load->model('fido/advertisement');
		if (($this->request->server['REQUEST_METHOD'] == 'POST') && ($this->validateForm())) {
			$this->model_fido_advertisement->addAdvertisement($this->request->post);
			$this->session->data['success'] = $this->language->get('text_success');
			$this->redirect(HTTPS_SERVER . 'index.php?route=extension/module&token=' . $this->session->data['token']);
		}
		$this->getForm();
	}

	public function update() {
		$this->load->language('module/advertisement');
		$this->document->title = $this->language->get('heading_title');
		$this->load->model('fido/advertisement');
		if (($this->request->server['REQUEST_METHOD'] == 'POST') && ($this->validateForm())) {
			$this->model_fido_advertisement->editAdvertisement($this->request->get['logo_id'], $this->request->post);
			$this->session->data['success'] = $this->language->get('text_success');
			$this->redirect(HTTPS_SERVER . 'index.php?route=extension/module&token=' . $this->session->data['token']);
		}
		$this->getForm();
	}

	public function delete() {
		$this->load->language('module/advertisement');
		$this->document->title = $this->language->get('heading_title');
		$this->load->model('fido/advertisement');
		if ((isset($this->request->post['selected'])) && ($this->validateDelete())) {
			foreach ($this->request->post['selected'] as $logo_id) {
				$this->model_fido_advertisement->deleteAdvertisement($logo_id);
			}
			$this->session->data['success'] = $this->language->get('text_success');
			$this->redirect(HTTPS_SERVER . 'index.php?route=extension/module&token=' . $this->session->data['token']);
		}
		$this->getList();
	}

	private function getList() {
		$this->load->language('module/advertisement');
		$this->load->model('fido/advertisement');

		$this->data['heading_title'] = $this->language->get('heading_title');

		$this->data['text_enabled'] = $this->language->get('text_enabled');
		$this->data['text_disabled'] = $this->language->get('text_disabled');
		$this->data['text_module_disabled'] = $this->language->get('text_module_disabled');
		$this->data['text_left'] = $this->language->get('text_left');
		$this->data['text_right'] = $this->language->get('text_right');
		$this->data['text_date_added'] = $this->language->get('text_date_added');
		$this->data['text_no_results'] = $this->language->get('text_no_results');

		$this->data['column_logo'] = $this->language->get('column_logo');
		$this->data['column_status'] = $this->language->get('column_status');
		$this->data['column_active'] = $this->language->get('column_active');
		$this->data['column_date_added'] = $this->language->get('column_date_added');
		$this->data['column_logo_hits'] = $this->language->get('column_logo_hits');
		$this->data['column_action'] = $this->language->get('column_action');
		$this->data['column_logo_position'] = $this->language->get('column_logo_position');

		$this->data['entry_limit'] = $this->language->get('entry_limit');
		$this->data['entry_position'] = $this->language->get('entry_position');
		$this->data['entry_status'] = $this->language->get('entry_status');
		$this->data['entry_sort_order'] = $this->language->get('entry_sort_order');

		$this->data['button_save'] = $this->language->get('button_save');
		$this->data['button_cancel'] = $this->language->get('button_cancel');
		$this->data['button_insert'] = $this->language->get('button_insert');
		$this->data['button_delete'] = $this->language->get('button_delete');

		$this->data['tab_general']  = $this->language->get('tab_general');
		$this->data['tab_data']  = $this->language->get('tab_data');

		if (isset($this->error['warning'])) {
			$this->data['error_warning'] = $this->error['warning'];
		} else {
			$this->data['error_warning'] = '';
		}

		$this->document->breadcrumbs = array();
		$this->document->breadcrumbs[] = array(
			'href'      => HTTPS_SERVER . 'index.php?route=common/home&token=' . $this->session->data['token'],
			'text'      => $this->language->get('text_home'),
			'separator' => FALSE
		);
		$this->document->breadcrumbs[] = array(
			'href'      => HTTPS_SERVER . 'index.php?route=extension/module&token=' . $this->session->data['token'],
			'text'      => $this->language->get('text_module'),
			'separator' => ' :: '
		);
		$this->document->breadcrumbs[] = array(
			'href'      => HTTPS_SERVER . 'index.php?route=module/advertisement&token=' . $this->session->data['token'],
			'text'      => $this->language->get('heading_title'),
			'separator' => ' :: '
		);

		$this->data['action']  = HTTPS_SERVER . 'index.php?route=module/advertisement&token=' . $this->session->data['token'];
		$this->data['newlogo'] = HTTPS_SERVER . 'index.php?route=module/advertisement/new_logo&token=' . $this->session->data['token'];
		$this->data['results'] = HTTPS_SERVER . 'index.php?route=module/advertisement/results&token=' . $this->session->data['token'];
		$this->data['change']  = HTTPS_SERVER . 'index.php?route=module/advertisement/change&token=' . $this->session->data['token'];
		$this->data['cancel']  = HTTPS_SERVER . 'index.php?route=extension/module&token=' . $this->session->data['token'];
		$this->data['insert'] = HTTPS_SERVER . 'index.php?route=module/advertisement/insert&token=' . $this->session->data['token'];
		$this->data['delete'] = HTTPS_SERVER . 'index.php?route=module/advertisement/delete&token=' . $this->session->data['token'];

		$this->data['logos'] = array();
		$results = $this->model_fido_advertisement->getAdvertisements();
		$this->load->model('tool/image');
		foreach ($results as $result) {
			$action = array();
			$action[] = array(
				'text' => $this->language->get('text_edit'),
				'href' => HTTPS_SERVER . 'index.php?route=module/advertisement/update&logo_id=' . $result['logo_id'] . '&token=' . $this->session->data['token']
			);
			
			$cfg_media_objects	= array(
			'swf'			=>	'<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=7,0,19,0" width="[_WIDTH_]" height="[_HEIGHT_]">
									<param name="movie" value="[_FILENAME_]">
									<param name="quality" value="high">
									<embed src="[_FILENAME_]" quality="high" pluginspage="http://www.macromedia.com/go/getflashplayer" type="application/x-shockwave-flash" width="[_WIDTH_]" height="[_HEIGHT_]"></embed>
								</object>'
			);
			
			if ( @substr($result["logo_file"], -4) == '.swf' ){
				if (substr($result["logo_file"], 0, 7) != 'http://'){
					$result["logo_file"]		= HTTP_IMAGE . $result["logo_file"];
				}
				$swf_width	= $result['logo_width'];
				$swf_height	= $result['logo_height'];
				$image		= str_replace("[_FILENAME_]", $result["logo_file"], $cfg_media_objects['swf']);
				$image		= str_replace("[_WIDTH_]", $swf_width, $image);
				$image		= str_replace("[_HEIGHT_]", $swf_height, $image);
			}
			else{
				
				if ( $result["logo_width"] || $result["logo_height"] ){
					if ($result['logo_file'] && file_exists(DIR_IMAGE . $result['logo_file'])) {
						$cfg_image = $this->model_tool_image->resize($result['logo_file'], $result["logo_width"], $result["logo_height"]);
					} else {
						$cfg_image = $this->model_tool_image->resize('no_image.jpg', $result["logo_width"], $result["logo_height"]);
					}
				}
				else{
					if ($result['logo_file'] && file_exists(DIR_IMAGE . $result['logo_file'])) {
						$cfg_image = $this->model_tool_image->resize($result['logo_file'], 200, 200);
					} else {
						$cfg_image = $this->model_tool_image->resize('no_image.jpg', 200, 200);
					}
				}
				$image='<img src="'.$cfg_image.'" title="'. $result['site_name'] .'">';				
			}
			
			$this->data['logos'][] = array(
				'logo_id'    	=> $result['logo_id'],
				'logo'   	 	=> $image,
				'hits'			=> $result['hits'],
				'status'		=> ($result['enabled'] ? $this->language->get('text_enabled') : $this->language->get('text_disabled')),
				'active'		=> ($result['logo_active'] ? $this->language->get('text_active') : $this->language->get('text_inactive')),
				'date_added' 	=> date($this->language->get('date_format_short'), strtotime($result['start_date'])),
				'selected'   	=> isset($this->request->post['selected']) && in_array($result['logo_id'], $this->request->post['selected']),
				'action'     	=> $action
			);
		}

		$this->data['positions'] = array();
		$this->data['positions'][] = array(
			'position' => 'bottom',
			'title'    => $this->language->get('text_bottom'),
		);
		$this->data['positions'][] = array(
			'position' => 'right',
			'title'    => $this->language->get('text_right'),
		);

		if (isset($this->request->post['logo_position'])) {
			$this->data['advertisement_position'] = $this->request->post['advertisement_position'];
		} else {
			$this->data['advertisement_position'] = $this->config->get('advertisement_position');
		}
		if (isset($this->request->post['advertisement_status'])) {
			$this->data['advertisement_status'] = $this->request->post['advertisement_status'];
		} else {
			$this->data['advertisement_status'] = $this->config->get('advertisement_status');
		}
		if (isset($this->request->post['advertisement_sort_order'])) {
			$this->data['advertisement_sort_order'] = $this->request->post['advertisement_sort_order'];
		} else {
			$this->data['advertisement_sort_order'] = $this->config->get('advertisement_sort_order');
		}
		

		$this->template = 'module/advertisement.tpl';
		$this->children = array(
			'common/header',
			'common/footer'
		);
		$this->response->setOutput($this->render(TRUE), $this->config->get('config_compression'));
	}

	private function getForm() {
		$this->load->language('module/advertisement');
		$this->load->model('fido/advertisement');

		$this->data['heading_title'] = $this->language->get('heading_title');
		$this->data['text_image_manager'] = $this->language->get('text_image_manager');
		$this->data['column_logo'] = $this->language->get('column_logo');
		$this->data['text_enabled'] = $this->language->get('text_enabled');
		$this->data['text_disabled'] = $this->language->get('text_disabled');
		$this->data['text_active'] = $this->language->get('text_active');
		$this->data['text_inactive'] = $this->language->get('text_inactive');
		$this->data['text_none'] = $this->language->get('text_none');
		$this->data['text_new_logo'] = $this->language->get('text_new_logo');
		$this->data['text_no_votes'] = $this->language->get('text_no_votes');
		$this->data['text_default'] = $this->language->get('text_default');
		$this->data['text_size'] = $this->language->get('text_size');
		$this->data['text_logo_width'] = $this->language->get('text_logo_width');
		$this->data['text_logo_height'] = $this->language->get('text_logo_height');
		$this->data['text_start_date'] = $this->language->get('text_start_date');
		$this->data['text_end_date'] = $this->language->get('text_end_date');
		$this->data['text_website_name'] = $this->language->get('text_website_name');
		$this->data['text_site_url'] = $this->language->get('text_site_url');
		$this->data['text_logo_pos'] = $this->language->get('text_logo_pos');
		$this->data['entry_logo'] = $this->language->get('entry_logo');
		$this->data['entry_store'] = $this->language->get('entry_store');
		$this->data['entry_status'] = $this->language->get('entry_status');
		$this->data['entry_active'] = $this->language->get('entry_active');
		$this->data['text_bottom'] = $this->language->get('text_bottom');
		$this->data['text_right'] = $this->language->get('text_right');
		$this->data['button_save'] = $this->language->get('button_save');
		$this->data['button_cancel'] = $this->language->get('button_cancel');
		$this->data['button_remove'] = $this->language->get('button_remove');
		$this->data['tab_general'] = $this->language->get('tab_general');
		$this->data['tab_data'] = $this->language->get('tab_data');
		$this->data['tab_results'] = $this->language->get('tab_results');
		$this->data['token'] = $this->session->data['token'];
		
		
		// End extraction of results

		if (isset($this->error['warning'])) {
			$this->data['error_warning'] = $this->error['warning'];
		} else {
			$this->data['error_warning'] = '';
		}
		if (isset($this->error['name'])) {
			$this->data['error_name'] = $this->error['name'];
		} else {
			$this->data['error_name'] = '';
		}
		
		if (isset($this->error['active_logo'])) {
			$this->data['error_active_logo'] = $this->error['active_logo'];
		} else {
			$this->data['error_active_logo'] = '';
		}
		$this->load->model('tool/image');
		if ( file_exists(DIR_IMAGE . 'no_image.jpg')) {				
				$image = $this->model_tool_image->resize('no_image.jpg', 150, 150);
		}else{
			$image='';
		}
		$cfg_media_objects	= array(
		'swf'			=>	'<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=7,0,19,0" width="[_WIDTH_]" height="[_HEIGHT_]" id="preview">
								<param name="movie" value="[_FILENAME_]">
								<param name="quality" value="high">
								<embed src="[_FILENAME_]" quality="high" pluginspage="http://www.macromedia.com/go/getflashplayer" type="application/x-shockwave-flash" width="[_WIDTH_]" height="[_HEIGHT_]"></embed>
							</object><div id="change_logo" class="buttons"><span><a onclick="image_upload(\'image\', \'preview\');" class="button">Chọn Logo</a></span></div>'
		);

  		$this->document->breadcrumbs = array();
  		$this->document->breadcrumbs[] = array(
     		'href'      => HTTPS_SERVER . 'index.php?route=common/home&token=' . $this->session->data['token'],
     		'text'      => $this->language->get('text_home'),
     		'separator' => FALSE
  		);
		$this->document->breadcrumbs[] = array(
			'href'      => HTTPS_SERVER . 'index.php?route=extension/module&token=' . $this->session->data['token'],
			'text'      => $this->language->get('text_module'),
			'separator' => ' :: '
		);
  		$this->document->breadcrumbs[] = array(
     		'href'      => HTTPS_SERVER . 'index.php?route=module/advertisement&token=' . $this->session->data['token'],
     		'text'      => $this->language->get('heading_title'),
     		'separator' => ' :: '
  		);

		if (!isset($this->request->get['logo_id'])) {
    		$this->data['new_logo'] = TRUE;
			$this->data['action'] = HTTPS_SERVER . 'index.php?route=module/advertisement/insert&token=' . $this->session->data['token'];
		} else {
			$this->data['action'] = HTTPS_SERVER . 'index.php?route=module/advertisement/update&logo_id=' . $this->request->get['logo_id'] . '&token=' . $this->session->data['token'];
		}
		$this->data['cancel'] = HTTPS_SERVER . 'index.php?route=module/advertisement&token=' . $this->session->data['token'];

		if ((isset($this->request->get['logo_id'])) && ($this->request->server['REQUEST_METHOD'] != 'POST')) {
     		$logo_info = $this->model_fido_advertisement->getAdvertisement($this->request->get['logo_id']);
    	}
		
		$this->load->model('localisation/language');
		$this->data['languages'] = $this->model_localisation_language->getLanguages();
		
		if(isset($logo_info)){
			if ( @substr($logo_info["logo_file"], -4) == '.swf' ){
					if (substr($logo_info["logo_file"], 0, 7) != 'http://'){
						$logo_info["logo_file"]		= HTTP_IMAGE . $logo_info["logo_file"];
					}
					$swf_width	= $logo_info['logo_width'];
					$swf_height	= $logo_info['logo_height'];
					$URLimage		= str_replace("[_FILENAME_]", $logo_info["logo_file"], $cfg_media_objects['swf']);
					$URLimage		= str_replace("[_WIDTH_]", $swf_width, $URLimage);
					$URLimage		= str_replace("[_HEIGHT_]", $swf_height, $URLimage);
				}
				else{
					
					if ( $logo_info["logo_width"] || $logo_info["logo_height"] ){
						if ($logo_info['logo_file'] && file_exists(DIR_IMAGE . $logo_info['logo_file'])) {
							$cfg_image = $this->model_tool_image->resize($logo_info['logo_file'], $logo_info["logo_width"], $logo_info["logo_height"]);
						} else {
							$cfg_image = $this->model_tool_image->resize('no_image.jpg', $logo_info["logo_width"], $logo_info["logo_height"]);
						}
					}
					else{
						if ($logo_info['logo_file'] && file_exists(DIR_IMAGE . $logo_info['logo_file'])) {
							$cfg_image = $this->model_tool_image->resize($logo_info['logo_file'], 200, 200);
						} else {
							$cfg_image = $this->model_tool_image->resize('no_image.jpg', 200, 200);
						}
					}
				$URLimage='<img src="'.$cfg_image.'" title="'. $logo_info['logo_file'] .'" id="preview" class="image" onclick="image_upload(\'image\', \'preview\');">';				
			}
		}else{
			$cfg_image = $this->model_tool_image->resize('no_image.jpg', 200, 200);
			$URLimage='<img src="'.$cfg_image.'"  id="preview" class="image" onclick="image_upload(\'image\', \'preview\');">';	
		}
		$this->data['URLimage']=$URLimage;
		if (isset($this->request->post['image'])) {
			$this->data['image'] = $this->request->post['image'];
		} elseif (isset($logo_info)) {
			$this->data['image'] = $logo_info['logo_file'];
		} else {
			$this->data['image'] = $image;
		}
		$this->load->model('setting/store');
		$this->data['stores'] = $this->model_setting_store->getStores();

		if (isset($this->request->post['logo_store'])) {
			$this->data['logo_store'] = $this->request->post['logo_store'];
		} elseif (isset($logo_info)) {
			$this->data['logo_store'] = $this->model_fido_advertisement->getAdvertisementStores($this->request->get['logo_id']);
		} else {
			$this->data['logo_store'] = array(0);
		}
		if (isset($this->request->post['site_name'])) {
			$this->data['site_name'] = $this->request->post['site_name'];
		} elseif (isset($logo_info)) {
			$this->data['site_name'] = $this->model_fido_advertisement->getAdvertisementDescriptions($this->request->get['logo_id']);;
		} else {
			$this->data['site_name'] = array();
		}
		if (isset($this->request->post['site_url'])) {
			$this->data['site_url'] = $this->request->post['site_url'];
		} elseif (isset($logo_info)) {
			$this->data['site_url'] = $logo_info['site_url'];
		} else {
			$this->data['site_url'] = '';
		}
		if (isset($this->request->post['logo_width'])) {
			$this->data['logo_width'] = $this->request->post['logo_width'];
		} elseif (isset($logo_info)) {
			$this->data['logo_width'] = $logo_info['logo_width'];
		} else {
			$this->data['logo_width'] = '';
		}
		if (isset($this->request->post['logo_height'])) {
			$this->data['logo_height'] = $this->request->post['logo_height'];
		} elseif (isset($logo_info)) {
			$this->data['logo_height'] = $logo_info['logo_height'];
		} else {
			$this->data['logo_height'] = '';
		}
		if (isset($this->request->post['start_date'])) {
       		$this->data['start_date'] = $this->request->post['start_date'];
		} elseif (isset($logo_info)) {
			$this->data['start_date'] = date('Y-m-d', strtotime($logo_info['start_date']));
		} else {
			$this->data['start_date'] = date('Y-m-d', time()-86400);
		}
		if (isset($this->request->post['end_date'])) {
       		$this->data['end_date'] = $this->request->post['end_date'];
		} elseif (isset($logo_info)) {
			$this->data['end_date'] = date('Y-m-d', strtotime($logo_info['end_date']));
		} else {
			$this->data['end_date'] = date('Y-m-d', time()-86400);
		}
				
		if (isset($this->request->post['status'])) {
			$this->data['status'] = $this->request->post['status'];
		} elseif (isset($logo_info)) {
			$this->data['status'] = $logo_info['enabled'];
		} else {
			$this->data['status'] = '';
		}
		if (isset($this->request->post['active'])) {
			$this->data['active'] = $this->request->post['active'];
		} elseif (isset($logo_info)) {
			$this->data['active'] = $logo_info['logo_active'];
		} else {
			$this->data['active'] = '';
		}
		
		$this->template = 'module/advertisement/form.tpl';
		$this->children = array(
			'common/header',
			'common/footer'
		);
		$this->response->setOutput($this->render(TRUE), $this->config->get('config_compression'));
	}

	private function validate() {
		if (!$this->user->hasPermission('modify', 'module/advertisement')) {
			$this->error['warning'] = $this->language->get('error_permission');
		}
		if (!$this->error) {
			return TRUE;
		} else {
			return FALSE;
		}	
	}

	private function validateForm() {
		if (!$this->user->hasPermission('modify', 'module/advertisement')) {
			$this->error['warning'] = $this->language->get('error_permission');
		}
		
		foreach ($this->request->post['site_name'] as $language_id => $value) {
			if ((strlen(utf8_decode($value['name'])) < 2) || (strlen(utf8_decode($value['name'])) > 255)) {
				$this->error['name'][$language_id] = $this->language->get('error_advertisement');
			}
			
		}
			
		
		if (!$this->error) {
			return TRUE;
		} else {
			return FALSE;
		}
	}

	private function validateDelete() {
		if (!$this->user->hasPermission('modify', 'module/advertisement')) {
			$this->error['warning'] = $this->language->get('error_permission');
		}
		if (!$this->error) {
			return TRUE; 
		} else {
			return FALSE;
		}
	}
}
?>
